import { defineStore } from 'pinia'
import { getValInObj } from '@/assets/utils/utils'

const useMainStore = defineStore('useMainStore', {
  state: () => {
    return {
      collapsed: false,
      weather: '',
      navTagList: [],
      todoList: [
        { id: 0, title: '写100行代码', checked: false },
        { id: 1, title: '改完全部BUG', checked: false },
        { id: 2, title: '写1000个方法', checked: true },
        { id: 3, title: '写10000个样式', checked: false }
      ],
      message: {
        unreadList: [
          {
            id: 0,
            title: '今晚吃小龙虾',
            time: '2022-05-01 00:00:01',
            description: '来自系统消息'
          },
          {
            id: 1,
            title: '明晚吃大闸蟹',
            time: '2022-05-14 00:00:01',
            description: '来自系统消息'
          },
          {
            id: 2,
            title: '上线！',
            time: '2022-02-23 00:00:01',
            description: '来自系统消息'
          },
          {
            id: 3,
            title: '明天玩一天LOL',
            time: '2022-03-12 00:00:01',
            description: '来自系统消息'
          }
        ],
        readList: [
          {
            id: 0,
            title: '5号看NBA',
            time: '2022-05-01 00:00:01',
            description: '来自系统消息'
          },
          {
            id: 1,
            title: '10号MSI',
            time: '2022-05-01 00:00:01',
            description: '来自系统消息'
          },
          {
            id: 2,
            title: '20号起飞',
            time: '2022-05-01 00:00:01',
            description: '来自系统消息'
          }
        ],
        recycleList: [
          {
            id: 0,
            title: '学一下Springboot',
            time: '2022-05-01 00:00:01',
            description: '来自系统消息'
          }
        ]
      }
    }
  },
  getters: {},
  actions: {
    handle_collapsed() {
      this.collapsed = !this.collapsed
    },
    handle_setTag(title: string, path: string) {
      const result = getValInObj('title', title, this.navTagList)
      if (!result) {
        ;(this.navTagList as any[]).push({ title, path })
      }
    },
    handle_closeTag(title: string) {
      const result = getValInObj('title', title, this.navTagList)
      const index = this.navTagList.indexOf(result as never)
      this.navTagList.splice(index, 1)
    },
    handle_todoCheckChange(id: number) {
      getValInObj('id', id, this.todoList).checked = !getValInObj(
        'id',
        id,
        this.todoList
      ).checked
    },
    handle_todoInputChange(value: string, id: number) {
      getValInObj('id', id, this.todoList).title = value
    }
  }
})

export default useMainStore
